package edu.nf.vac.vaccination.service;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import edu.nf.vac.vaccination.dao.AreaDao;
import edu.nf.vac.vaccination.dao.VaccinationDao;
import edu.nf.vac.vaccination.dto.AreaDTO;
import edu.nf.vac.vaccination.dto.VaccinationDTO;
import edu.nf.vac.vaccination.dto.VaccinationPageDTO;
import edu.nf.vac.vaccination.entity.Area;
import edu.nf.vac.vaccination.entity.Vaccination;
import edu.nf.vac.vaccination.exception.DataAccessException;
import edu.nf.vac.vaccination.inf.VaccinationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author : PYY
 * @create 2022/3/30
 */
@Slf4j
@DubboService(interfaceClass = VaccinationService.class,version = "1.0.0")
@Transactional(rollbackFor = RuntimeException.class)
//@Service
public class VaccinationServiceImpl implements VaccinationService{

    @Autowired
    private AreaDao areaDao ;
    @Autowired
    private VaccinationDao vaccinationDao ;

    /**
     * 查询所有区域
     * @return
     */
    @Override
    public List<AreaDTO> listArea() {
        List<Area> areaList = areaDao.listArea();
        List<AreaDTO> dtoList = BeanUtil.copyToList(areaList, AreaDTO.class);
        return dtoList ;
    }

    /**
     * 根据区域查询接种点
     * @param areaId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public VaccinationPageDTO listVaccination(Integer areaId, Integer pageNum, Integer pageSize) {
        List<Vaccination> list = vaccinationDao.listVaccination(areaId, pageNum, pageSize);
        PageInfo<Vaccination> pageInfo = new PageInfo<>(list);
        VaccinationPageDTO pageDTO = BeanUtil.copyProperties(pageInfo, VaccinationPageDTO.class);
        return pageDTO ;
    }

    /**
     * 根据id查询接种点
     * @param id
     * @return
     */
    @Override
    public VaccinationDTO getVaccinationById(Integer id) {
        Vaccination vaccination = vaccinationDao.getVaccinaionById(id);
        VaccinationDTO vaccinationDTO = BeanUtil.copyProperties(vaccination, VaccinationDTO.class);
        return vaccinationDTO ;
    }

    /**
     * 添加接种点
     * @param vaccinationDTO
     */
    @Override
    public void saveVaccination(VaccinationDTO vaccinationDTO) {

        try {
            Vaccination vaccination = BeanUtil.copyProperties(vaccinationDTO, Vaccination.class);
            vaccinationDao.saveVaccination(vaccination);
        } catch (RuntimeException e) {
            log.error(e.getMessage(),e);
            throw new DataAccessException("保存接种点异常，请稍后重试");
        }
    }

    /**
     * 修改接种点
     * @param vaccinationDTO
     */
    @Override
    public void updateVaccination(VaccinationDTO vaccinationDTO) {
        try {
            Vaccination vaccination = BeanUtil.copyProperties(vaccinationDTO, Vaccination.class);
            vaccinationDao.updateVaccination(vaccination);
        } catch (RuntimeException e) {
            log.error(e.getMessage(),e);
            throw new DataAccessException("更新接种点异常，请稍后重试");
        }
    }
}